// pages/video/video.js
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    videoGroupList:[],//导航标签列表
    navId: '',//导航标识
    videoList:[],//某个分类下的视频列表
    videoId:'',//视频标识
    videoUrl:'',//已获取的视频链接
    videoUpdateTime:[],//记录video播放的时长
    isTriggered:false,//标识下拉刷新状态
    offset:0,//视频列表页码
  },
  onLoad(options) {
    //获取导航数据
    this.getVideoGroupList()
  },
  //获取videoGroupList的数据
  async getVideoGroupList(){
    let videoGroupListData = await request('/video/group/list')
    this.setData({
      videoGroupList:videoGroupListData.data.slice(0,14),
      navId:videoGroupListData.data[0].id//设置初始选中状态
    })
    // 获取视频列表数据，放在此处是确保navId有值
    this.getVideoList(this.data.navId)
  },
  //获取视频列表数据
  async getVideoList(navId){
    let videoListData = await request('/video/group',{id:navId})
    let videoList = videoListData.datas
    /* let index = 0;
    let videoList = videoListData.datas.map(item => {
      item.id = index++;
      return item;
    }) */
    //关闭数据加载提示
    wx.hideLoading()
    this.setData({
      videoList,
      isTriggered:false//关闭下拉刷新
    })
  },
  //点击改变navId来改变选中状态
  changeNavId(event){
    let navId = event.currentTarget.id;
    this.setData({
      navId:navId>>>0,
      videoList:[]
    })
    //提示加载中
    wx.showLoading({
      title:'正在加载中'
    })
    this.getVideoList(this.data.navId)
  },
  //获取单个视频数据
  async getVideoUrl(videoId){
    let videoUrlData = await request('/video/url',{id:videoId})
    this.setData({
      videoUrl:videoUrlData.urls[0].url
    })
  },
  //点击改变videoId来播放某个视频，同时判断是否播放过，已播放则给定播放进度
  changeVideoId(event){
    this.setData({
      videoUrl:'',
    })
    let videoId = event.currentTarget.id
    this.setData({
      videoId,
    })
    this.getVideoUrl(this.data.videoId)
  },
  //视频开始或继续播放视频回调
  handlePlay(event){
    let vid = event.currentTarget.id
    //创建控制video标签的实例对象
    this.videoContext = wx.createVideoContext(vid);
    //判断视频之前是否播放过
    let {videoUpdateTime} = this.data;
    let videoPlayItem = videoUpdateTime.find(item => item.vid === vid)
    if(videoPlayItem){
      this.videoContext.seek(videoPlayItem.currentTime);
    }
  },
  //视频结束播放回调
  handleEnded(event){
    //移除当前视频的播放记录
    let {videoUpdateTime} = this.data;
    videoUpdateTime.splice(videoUpdateTime.findIndex(item => item.vid === event.currentTarget.id),1)
    this.setData({
      videoUpdateTime
    })
  },
  //监听视频播放进度
  handleTimeUpdate(event){
    let videoTimeObj = {vid:event.currentTarget.id,currentTime:event.detail.currentTime}
    let {videoUpdateTime} = this.data
    //判断数组中是否已存在该视频的播放记录
    let videoPlayItem = videoUpdateTime.find(item => item.vid === videoTimeObj.vid)
    if(videoPlayItem){
      videoPlayItem.currentTime = event.detail.currentTime
    }else{
      videoUpdateTime.push(videoTimeObj)
    }
    this.setData({
      videoUpdateTime
    })
  },
  //下拉刷新回调
  handleRefresher(){
    //获取最新的视频列表
    this.getVideoList(this.data.navId)
  },
  //上拉触底回调
  async handleToLower(){
    //数据分页
    let {offset} = this.data
    offset++
    let videoListData = await request('/video/group',{id:this.data.navId,offset})
    //获取新页码的视频列表
    let videoAddList = videoListData.datas
    let videoList = this.data.videoList
    //将新列表加到原有列表中
    videoList.push(...videoAddList)
    //更新数据
    this.setData({
      offset,
      videoList
    })
  },
  //点击跳转到搜索页
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    // console.log('下拉动作')
    this.getVideoGroupList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    // console.log('上拉触底')
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title:'芊芊云音乐',
      page:'/pages/video/video'
    }
  }
})